home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / gfx / show / gs503_data.lha / Ghostscript / data / pdf_2ps.ps < prev    next >
Text File  |  1997-08-14  |  8KB  |  274 lines

  1. %    Copyright (C) 1994, 1996 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % pdf_2ps.ps
  16. % PDF to PostScript additions to PDF reader.
  17.  
  18. GS_PDF_ProcSet begin
  19. pdfdict begin
  20.  
  21. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  22. .currentglobal true .setglobal
  23.  
  24. userdict /pdf2psdict 30 dict put
  25. pdf2psdict begin
  26.  
  27. % Generate a sufficiently unique name (at least unique within the current
  28. % save/restore environment).
  29. /uniqueid#
  30.  { userdict length
  31.     { dup neg =string cvs dup 0 (_) putinterval cvn
  32.       userdict 1 index known not { exch pop exit } if pop 1 add
  33.     }
  34.    loop
  35.  } bdef
  36.  
  37. % "Wrap" all the runtime operators so they call #exec.
  38. % Make sure we have enough room in the current dictionary for this.
  39. currentdict dup maxlength numargsdict length add .setmaxlength
  40. numargsdict
  41.  { 1 index load exch 2 index exch /#exec cvx 4 packedarray cvx def
  42.  }
  43. forall
  44. /pdfmark
  45.   /pdfmark load dup type /operatortype eq { 1 packedarray cvx } if
  46.    { /pdfmark counttomark 1 sub #exec } bind
  47.   aload length 1 add packedarray cvx
  48. def
  49. % Define #exec so it also prints out its arguments.
  50. /dictwrite#        % <file> <dict> dictwrite# -
  51.  { dup length 240 le
  52.     { 1 index (mark ) writestring
  53.        { exch 2 index exch write#
  54.      1 index ( ) writestring 1 index exch write#
  55.      2 index (\n) writestring
  56.         }
  57.       forall dup (.dicttomark) writestring
  58.     }
  59.     { 2 copy length write=only 1 index ( dict\n) writestring
  60.        { exch 2 index dup (dup ) writestring exch write#
  61.      1 index dup ( ) writestring exch write#
  62.      dup ( put\n) writestring
  63.        }
  64.       forall
  65.     }
  66.    ifelse pop
  67.  } bdef
  68. /fontwrite#        % <file> <font> fontwrite# -
  69.  {        % Find the named font and then modify it.
  70.    2 copy /FontName get =string cvs
  71.     { dup dup length 1 sub 1 getinterval (%) ne { exit } if
  72.       0 1 index length 1 sub getinterval
  73.     }
  74.    loop cvn write===only 1 index ( findfont ) writestring
  75.         % Load the appropriate Encoding, by name if possible.
  76.    dup /Encoding get
  77.    dup dup StandardEncoding eq exch ISOLatin1Encoding eq or
  78.         % Stack: file font encoding stdbool
  79.     { StandardEncoding eq { (StandardEncoding) } { (ISOLatin1Encoding) } ifelse
  80.       2 index exch writestring
  81.     }
  82.     { 2 index exch write#
  83.     }
  84.    ifelse
  85.         % Check for modified Metrics.
  86.    dup /Metrics .knownget
  87.     { 2 index ( ) writestring
  88.       2 index exch write#
  89.     }
  90.     { 1 index ( null) writestring
  91.     }
  92.    ifelse
  93.    1 index ( .updatefont { /_ exch definefont } if\n) writestring
  94.    pop pop
  95.  } bdef
  96. /write#dict 10 dict dup begin
  97.   /arraytype
  98.    { dup xcheck { (}) ({) } { (]) ([) } ifelse
  99.      2 index length 0 eq
  100.       { 3 index exch writestring exch
  101.       }
  102.       { 3 -1 roll
  103.      { 3 index 2 index writestring 3 index exch write# pop (\n) }
  104.     forall
  105.       }
  106.      ifelse pop writestring
  107.    } bdef
  108.   /dicttype
  109.    { null userdict { 3 index eq { exch pop exit } if pop } forall
  110.      dup null eq
  111.       { pop 2 copy dup /FID known { fontwrite# } { dictwrite# } ifelse
  112.     1 index ( userdict ) writestring
  113.     uniqueid# 2 index 1 index write# 2 index ( 2 index put) writestring
  114.     userdict exch 3 -1 roll put pop
  115.       }
  116.       { exch pop cvx write===only
  117.       }
  118.      ifelse
  119.    } bdef
  120.   /filetype
  121.    {        % Convert all files into currentfile, on the theory that
  122.         % any file-based data will be copied in-line.
  123.      pop (currentfile) writestring
  124.    } bdef
  125.   /marktype
  126.    { pop ([) writestring
  127.    } bdef
  128.   /packedarraytype
  129.    /arraytype load def
  130.   /realtype
  131.    { dup abs 16#ffffff le { dup dup cvi eq { cvi } if } if write=only
  132.    } bdef
  133. end def
  134. /write#
  135.  { dup type //write#dict exch .knownget { exec } { write===only } ifelse
  136.  } bind def
  137.  
  138. % Rebind the procedures that conditionally write out PostScript.
  139.  
  140. /#            % <arg1> ... <argN> <opname> <N> # -
  141.  { 1 index load 3 1 roll #exec
  142.  } bdef
  143. /#?            % - #? <writing>
  144.  { /PSout where { pop true } { false } ifelse
  145.  } bdef
  146. /defined#        % <name> defined# <bool>
  147.  { dup where { exch get } { pop false } ifelse
  148.  } bdef
  149. /#exec        % <arg1> ... <argN> <proc|operator> <opname> <N> #exec -
  150.  { /PSout where
  151.     { pop dup ([) eq { pop counttomark 1 sub } if
  152.       -1 1 { 1 add index PSout exch write# PSout ( ) writestring } for
  153.       PSout exch write=
  154.     }
  155.     { pop pop
  156.     }
  157.    ifelse exec
  158.  } bdef
  159. /#dsc        % mark <obj1> ... #dsc -
  160.  { /PSout where
  161.     { pop counttomark
  162.        { counttomark -1 roll PSout exch write=only }
  163.       repeat pop PSout (\n) writestring
  164.     }
  165.     { cleartomark
  166.     }
  167.    ifelse
  168.  } bdef
  169. /copyfile#    % <filename> copyfile# -
  170.     { findlibfile
  171.        { exch pop }
  172.        { (r) file }        % let the error happen
  173.       ifelse
  174.        { dup =string readline pop (%BEGIN) eq { exit } if
  175.        }
  176.       loop
  177.        { dup =string readline not { pop exit } if
  178.          dup (%END) eq { pop exit } if
  179.       { ( ) anchorsearch
  180.          { pop }
  181.          { (\t) anchorsearch { pop } { exit } ifelse }
  182.         ifelse
  183.       }
  184.      loop
  185.      dup () eq { true } { dup 0 1 getinterval (%) eq } ifelse
  186.       { pop }
  187.       { (%) search { exch pop exch pop } if mark exch #dsc }
  188.      ifelse
  189.        }
  190.       loop closefile
  191.     } bdef
  192. /#dscfile    % <filename> #dscfile -
  193.  { /PSout where
  194.     { pop /PSNoProcSet defined#
  195.        { PSout exch write===only PSout ( runlibfile\n) writestring }
  196.        { copyfile# }
  197.       ifelse
  198.     }
  199.     { pop
  200.     }
  201.    ifelse
  202.  } bdef
  203.  
  204. % Rebind Is, which constructs a data source for an image.
  205. % pdf_draw defined it to simply retrieve the stream.
  206. /ID_draw /ID load def
  207. /Is_draw /Is load def
  208. /EI_draw /EI load def
  209. userdict /Is_string null put    % establish a binding
  210. userdict /Is_data null put    % ditto
  211. /ID_proc1 {/ASCIIHexDecode filter} def        % no bind
  212. /ID_proc2 {/ASCII85Decode filter /RunLengthDecode filter} def    % no bind
  213. /ID
  214.  { /PSout where
  215.     { pop dup length 1 add dict copy
  216.       dup /FilterProc PSLevel1 { /ID_proc1 } { /ID_proc2 } ifelse load
  217.       /PSBinaryOK defined# { dup length 2 sub 2 exch getinterval } if
  218.       dup length 0 eq { pop pop pop } { put } ifelse
  219.     }
  220.    if ID_draw
  221.  } bdef
  222. /Is        % <imagedict> Is <imagedict> <datasource>
  223.  { /PSout where
  224.     { pop dup /DataSource get string /Is_string exch store
  225.       /Is_data [ PSout
  226.       PSLevel1
  227.        { /PSBinaryOK defined#
  228.       { /NullEncode } { /ASCIIHexEncode } ifelse
  229.      filter
  230.        }
  231.        { /PSBinaryOK defined#
  232.       { Is_string length /RunLengthEncode filter
  233.       }
  234.       { /ASCII85Encode filter
  235.         dup Is_string length /RunLengthEncode filter exch
  236.       }
  237.      ifelse
  238.        }
  239.       ifelse ] store
  240.       Is_draw
  241.        { Is_string readstring pop Is_data 0 get 1 index writestring }
  242.       aload length 1 add packedarray cvx
  243.     }
  244.     { Is_draw
  245.     }
  246.    ifelse
  247.  } bdef
  248. /EI
  249.  { /PSout where { pop Is_data { closefile } forall } { EI_draw } ifelse
  250.  } bdef
  251.  
  252. % Rebind readfontfilter, which constructs the filter that
  253. % reads the text of an embedded Type 1 (and eventually Type 3) font.
  254. /readfontfilter_orig /readfontfilter load def
  255. /readfontfilter {    % <proc|file> readfontfilter <filter>
  256.   dup type /filetype eq {
  257.     cvlit 100 string /readstring cvx /pop cvx 4 array astore cvx
  258.   } if
  259.   /copyfontdata cvx 2 array astore cvx
  260.   0 () /SubFileDecode filter
  261. } bdef
  262. /copyfontdata {        % <string> <origproc> copyfontdata <substring>
  263.   exec /PSout where { pop PSout 1 index writestring } if
  264. } bdef
  265.  
  266. currentdict readonly pop end    % pdf2psdict
  267.  
  268. .setglobal
  269. end        % pdfdict
  270. end        % GS_PDF_ProcSet
  271.